package com.mfnote.db.dao;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.mfnote.db.DBHelper;
import com.mfnote.db.DBInfo;
import com.mfnote.model.PaintEntity;

/**
 * Paint模块数据库操作类
 * 
 * @author ZHF
 * 
 */
public class PaintDao {
	public SQLiteDatabase db; // 系统提供的数据库
	private DBHelper dbHelper = null;

	public PaintDao(Context context) {
		dbHelper = DBHelper.getInstance(context);// 用static方法来实例化该对象
	}

	/** 打开数据库 **/
	private void openDatabase() {
		db = dbHelper.getWritableDatabase(); // 这里根据数据库名和版本号判断，第一次就调用onCreate创建表，第二次若两者相同就直接返回，若不同调用onUpgrade
	}

	/** 插入涂鸦数据 **/
	public long insertPaint(PaintEntity paintEntity) {
		// 打开数据库
		openDatabase();

		ContentValues values = new ContentValues();

		values.put("title", paintEntity.getTitle());
		values.put("imagePath", paintEntity.getImagePath());
		values.put("time", paintEntity.getTimeNow());

		long raw = db.insert(DBInfo.Table.TB_PAINT_NAME, null, values);

		db.close();
		return raw; // 返回的是当前受影响的行号( 不为-1即为插入成功)
	}

	/** 查询涂鸦表的所有记录 **/
	public ArrayList<HashMap<String, Object>> getPaintAllCount() {
		// 打开数据库
		openDatabase();

		ArrayList<HashMap<String, Object>> data = null; // 接收数据

		// Cursor----ResultSet:用于接收数据库数据列表
		Cursor cursor = db.query(DBInfo.Table.TB_PAINT_NAME, null, null, null,
				null, null, null);

		if (null != cursor && cursor.getCount() > 0) {
			data = new ArrayList<HashMap<String, Object>>();

			while (cursor.moveToNext()) {
				HashMap<String, Object> map = new HashMap<String, Object>();

				map.put("title",
						cursor.getString(cursor.getColumnIndex("title")));
				map.put("imagePath",
						cursor.getString(cursor.getColumnIndex("imagePath")));
				map.put("time", cursor.getString(cursor.getColumnIndex("time")));

				data.add(map);
			}
		} else {
			System.out.println("没有数据！");
		}
		cursor.close();
		db.close(); // 用完记得要关闭哦
		return data;
	}

	/** 更新涂鸦表的记录--根据标题名称 **/
	public long updatePaint(PaintEntity paintEntity) {

		// 打开数据库
		openDatabase();

		ContentValues values = new ContentValues();

		values.put("title", paintEntity.getTitle());
		values.put("imagePath", paintEntity.getImagePath());
		values.put("time", paintEntity.getTimeNow());

		long raws = db.update(DBInfo.Table.TB_PAINT_NAME, values, "title=?",
				new String[] { paintEntity.getTitle() });
		db.close(); // 用完记得要关闭哦
		return raws; // 返回的是当前受影响的行号
	}

	/** 删除涂鸦的记录 **/
	public long deletePaint(HashMap<String, Object> map) {
		// 打开数据库
		openDatabase();

		long raws = db.delete(DBInfo.Table.TB_PAINT_NAME, "title=?",
				new String[] { map.get("title").toString() });

		db.close();
		return raws; // 返回的是当前受影响的行号number of rows affected
	}

	/** 查询记录条数 **/
	public long queryCount() {
		// 打开数据库
		openDatabase();

		String sql = "select count() from " + DBInfo.Table.TB_PAINT_NAME;
		SQLiteStatement statement = db.compileStatement(sql);
		long count = statement.simpleQueryForLong();
		return count;
	}
		
	public ArrayList<HashMap<String, Object>> queryByTitle(String name) {
		// 打开数据库
			openDatabase();

			ArrayList<HashMap<String, Object>> data = null; // 接收数据

			// Cursor----ResultSet:用于接收数据库数据列表
			Cursor cursor = db.query(DBInfo.Table.TB_PAINT_NAME, null, "title=?", new String[]{name},null, null, null);

			if (null != cursor && cursor.getCount() > 0) {
				data = new ArrayList<HashMap<String, Object>>();

				while (cursor.moveToNext()) {
					HashMap<String, Object> map = new HashMap<String, Object>();

					map.put("title",cursor.getString(cursor.getColumnIndex("title")));
					map.put("imagePath",cursor.getString(cursor.getColumnIndex("imagePath")));
					map.put("time", cursor.getString(cursor.getColumnIndex("time")));
					
					data.add(map);
				}
			}
			cursor.close();
			db.close(); // 用完记得要关闭哦
			return data;
	}
	
}
